//
//  CalculateLoanTableViewCell.m
//  YiJuZhangShou
//
//  Created by URANUS on 16/3/19.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CalculateLoanTableViewCell.h"
#import "UITableViewCell+Custom.h"
#import "CalculateLoanTableViewModel.h"
#import "Masonry.h"

@interface CalculateLoanTableViewCell()
@property (nonatomic, strong) UILabel *numberLabel;
@property (nonatomic, strong) UILabel *monthlyRepaymentLabel;
@property (nonatomic, strong) UILabel *monthlyRepaymentPrincipalLabel;
@property (nonatomic, strong) UILabel *monthlyRepaymentInterestLabel;
@property (nonatomic, strong) UILabel *leftPrincipalLabel;
@property (nonatomic, strong) UILabel *advanceRepaymentLabel;
@end

@implementation CalculateLoanTableViewCell

#pragma mark Initialize
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setConstraints];
        self.backgroundColor = BackgroundColor240;
    }
    return self;
}

#pragma mark Private
- (void)setConstraints{
   [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.contentView).offset(5);
       make.width.equalTo(self.contentView).multipliedBy(0.11);
       make.top.bottom.equalTo(self.contentView);
       make.height.mas_equalTo(ScreenHeight*0.04);
   }];
   [self.monthlyRepaymentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.numberLabel.mas_right);
       make.width.equalTo(self.contentView).multipliedBy(0.15);
       make.top.bottom.equalTo(self.numberLabel);
   }];
   [self.monthlyRepaymentPrincipalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.monthlyRepaymentLabel.mas_right);
       make.top.bottom.equalTo(self.monthlyRepaymentLabel);
       make.width.equalTo(self.contentView).multipliedBy(0.17);
   }];
    [self.monthlyRepaymentInterestLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.monthlyRepaymentPrincipalLabel.mas_right);
        make.top.bottom.equalTo(self.monthlyRepaymentPrincipalLabel);
        make.width.equalTo(self.monthlyRepaymentPrincipalLabel);
    }];
   [self.leftPrincipalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
       make.left.equalTo(self.monthlyRepaymentInterestLabel.mas_right);
       make.top.bottom.equalTo(self.monthlyRepaymentInterestLabel);
   }];
    [self.advanceRepaymentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.leftPrincipalLabel.mas_right);
        make.top.bottom.equalTo(self.leftPrincipalLabel);
        make.width.equalTo(self.leftPrincipalLabel);
        make.right.equalTo(self.contentView).offset(-5);
    }];

}
#pragma mark Private
- (void)setUpLabel:(UILabel *)label{
    label.font = [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote];
    label.textAlignment = NSTextAlignmentCenter;
}


#pragma mark Getter
-(UILabel *)numberLabel{

    if (_numberLabel == nil) {
        _numberLabel = [UILabel new];
        [self setUpLabel:_numberLabel];
        [self.contentView addSubview:_numberLabel];
    }
    
    return _numberLabel;
}

-(UILabel *)monthlyRepaymentLabel{

    if (_monthlyRepaymentLabel == nil) {
        _monthlyRepaymentLabel = [UILabel new];
        [self setUpLabel:_monthlyRepaymentLabel];
        [self.contentView addSubview:_monthlyRepaymentLabel];
    }
    
    return _monthlyRepaymentLabel;
}

-(UILabel *)monthlyRepaymentPrincipalLabel{
 
    if (_monthlyRepaymentPrincipalLabel == nil) {
        _monthlyRepaymentPrincipalLabel = [UILabel new];
        [self setUpLabel:_monthlyRepaymentPrincipalLabel];
        [self.contentView addSubview:_monthlyRepaymentPrincipalLabel];
    }
    
    return _monthlyRepaymentPrincipalLabel;
}

-(UILabel *)monthlyRepaymentInterestLabel{

    if (_monthlyRepaymentInterestLabel == nil) {
        _monthlyRepaymentInterestLabel = [UILabel new];
        [self setUpLabel:_monthlyRepaymentInterestLabel];
        [self.contentView addSubview:_monthlyRepaymentInterestLabel];
    }
    
    return _monthlyRepaymentInterestLabel;
}

-(UILabel *)leftPrincipalLabel{

    if (_leftPrincipalLabel == nil) {
        _leftPrincipalLabel = [UILabel new];
        [self setUpLabel:_leftPrincipalLabel];
        [self.contentView addSubview:_leftPrincipalLabel];
    }
    return _leftPrincipalLabel;
}

-(UILabel *)advanceRepaymentLabel{

    if (_advanceRepaymentLabel == nil) {
        _advanceRepaymentLabel = [UILabel new];
        [self setUpLabel:_advanceRepaymentLabel];
        [self.contentView addSubview:_advanceRepaymentLabel];
    }
    return _advanceRepaymentLabel;
}

#pragma mark Setter
-(void)setModel:(CalculateLoanTableViewModel *)model{
    self.numberLabel.text = model.number;
    self.monthlyRepaymentLabel.text = [NSString stringWithFormat:@"%ld",[model.monthlyRepayment integerValue]];
    self.monthlyRepaymentPrincipalLabel.text = [NSString stringWithFormat:@"%ld",[model.monthlyRepaymentPrincipal integerValue]];
    self.monthlyRepaymentInterestLabel.text = [NSString stringWithFormat:@"%ld",[model.monthlyRepaymentInterest integerValue]];
    self.leftPrincipalLabel.text = [NSString stringWithFormat:@"%ld",[model.leftPrincipal integerValue]];
    self.advanceRepaymentLabel.text = [NSString stringWithFormat:@"%ld",[model.advanceRepayment integerValue]];
}

@end
